import * as path from "path";
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import { Configuration } from 'webpack';
import MyPlugin from "./plugins/MyPlugin";

const resolve = (url, base = __dirname) => path.resolve(base, url);
const ROOT_PATH = resolve('');
const SRC_PATH = resolve('src');
const DIST_PATH = resolve('dist');

console.log(process.env.NODE_ENV);

console.log(process.env.BUILD_TYPE);

const tsRule = (buildType) => {
    if (buildType === 'esbuild') {
       return {
            test: /\.ts$/,
            loader: 'esbuild-loader',
            options: {
                loader: 'ts',
                target: 'es2015',
            }
        }
    }

    return {
        test: /\.ts$/,
        use: 'ts-loader',
    }
}

const config: Configuration = {
    entry: {
        first: resolve('demo/index.ts', SRC_PATH),
    },
    mode: "development",
    resolve: {
        extensions: ['.ts']
    },
    plugins:[
        new CleanWebpackPlugin(),
        new MyPlugin({resource: ['lodash']}),
        // new HtmlWebpackPlugin({
        //     template:'./src/index.html'
        // })
    ],
    output: {
        path: DIST_PATH,
        filename: '[name]_[chunkhash].js'
    },
    module: {
        rules: [
            tsRule(process.env.BUILD_TYPE)
        ]
    }
}

export default config;
